import {
  isPicTitleDescArray,
  isStringArray,
  type OptionsProps,
  type OptionsStatusByIndexPayload,
  type PicLink,
  type TextProps,
  type TypeStatus,
} from '@/types';

/*
 * @Author: jhw
 * @Date: 2025-08-05 14:04:09
 * @LastEditors: jhw
 * @LastEditTime: 2025-08-15 13:13:38
 * @Description:
 */
/**
 * 设置文本状态
 * @param textProps 文本属性
 *  - status: 文本内容
 *  - isShow: 是否显示
 *  - configKey: 配置项的key
 *  - editCom: 编辑组件
 *  - id: 组件的id
 */
export const setTextStatus = (textProps: TextProps, text: string) => {
  textProps.status = text;
};

/**
 * 设置选项状态
 * @param textProps 选项属性
 *  - status: 选项内容
 *  - isShow: 是否显示
 *  - configKey: 配置项的key
 *  - editCom: 编辑组件
 *  - id: 组件的id
 */
export const setOptionsStatus = (optionProps: OptionsProps) => {
  if (isStringArray(optionProps.status)) {
    const lastValue = optionProps.status[optionProps.status.length - 1];
    const lastValueNum = lastValue.split('选项')[1] || 0;
    optionProps.status.push(`新增选项${Number(lastValueNum) + 1}`);
  } else if (optionProps.status && isPicTitleDescArray(optionProps.status)) {
    optionProps.status.push({
      picTitle: '图片标题',
      picDesc: '图片描述',
      value: '',
    });
  }
};

/**
 * 设置status数组中某一个图片链接
 * @param optionProps 选项属性
 */
export const setPicLinkByIndex = (optionProps: OptionsProps, payload: PicLink) => {
  if (optionProps.status && isPicTitleDescArray(optionProps.status)) {
    optionProps.status[payload.index].value = payload.link;
  }
};

export const removeOptionsStatus = (optionProps: OptionsProps, index: number) => {
  optionProps.status.splice(index, 1);
};

export const setPositionStatus = (optionProps: OptionsProps, value: number) => {
  optionProps.currentStatus = value;
};

export const setSizeStatus = (optionProps: OptionsProps, value: number) => {
  optionProps.currentStatus = value;
};

export const setTypeStatus = (optionProps: OptionsProps, value: number) => {
  optionProps.currentStatus = value;
};

//
export const setNoteDescTypeStatus = (status: TypeStatus, value: number) => {
  // status.type.setValue(value);
  if (status.type.currentStatus === value) return;
  status.type.currentStatus = value;
  // 所有状态全部取反
  status.title.isShow = !status.title.isShow;
  status.desc.isShow = !status.desc.isShow;
  status.position.isShow = !status.position.isShow;
  status.titleSize.isShow = !status.titleSize.isShow;
  status.descSize.isShow = !status.descSize.isShow;
  status.titleWeight.isShow = !status.titleWeight.isShow;
  status.descWeight.isShow = !status.descWeight.isShow;
};

export const setUse = (optionProps: OptionsProps, value: boolean) => {
  optionProps.isUse = value;
};

export const setOptionsStatusByIndex = (
  optionProps: OptionsProps,
  payload: OptionsStatusByIndexPayload,
) => {
  optionProps.status[payload.index] = payload.val;
};

export const setIsRequireStatus = (optionProps: OptionsProps, value: number) => {
  optionProps.currentStatus = value;
};

// 开关类设置操作
export const setSwitchStatus = (optionProps: OptionsProps, value: number) => {
  optionProps.currentStatus = value;
};
